package com.ccgk.logistics.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Company;
import com.ccgk.logistics.domain.CompanyExample;
import com.ccgk.logistics.domain.User;
import com.ccgk.logistics.domain.UserExample;
import com.ccgk.logistics.persistence.CompanyMapper;
import com.ccgk.logistics.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService{
	
	@Autowired
	private CompanyMapper companyMapper;
	
	@Override
	public Integer add(Company company) {
		return companyMapper.insert(company);
	}

	@Override
	public List<Company> findAll() {
		return companyMapper.selectByExample(null);
	}


	@Override
	public Company getById(int id) {
		return companyMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Company company) {
		return companyMapper.updateByPrimaryKey(company);
	}

	@Override
	public Integer delete(int id) {
		return companyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PaginationVo<Company> findList(CompanyCriteria criteria,
			int currentPage, int pageSize) {
		CompanyExample example = new CompanyExample();
		CompanyExample.Criteria _criteria = example.createCriteria();
		Map<Integer,Object> params = criteria.getParams();
		for(Integer key : params.keySet()){
			if(key.equals(1)){
				_criteria.andNameLike(params.get(key).toString());
			}
		}
		List<Company> data = companyMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = companyMapper.countByExample(example);
		PaginationVo<Company> result = new PaginationVo<Company>(data, recordCount, pageSize, currentPage);
		return result;
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
	}

	@Override
	public Company getByName(String name) {
		CompanyExample example = new CompanyExample();
		example.createCriteria().andNameEqualTo(name);
		List<Company> result = companyMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		else
			return result.get(0);
	}

	@Override
	public List<Company> findByName(String name) {
		CompanyExample example = new CompanyExample();
		example.createCriteria().andNameLikeBlear(name);
		List<Company> result = companyMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		else
			return result;
	}
	
	@Override
	public List<Company> findByType(String type){
		CompanyExample example = new CompanyExample();
		example.createCriteria().andTypeEqualTo(type);
		example.setOrderByClause("abbreviate asc");
		return companyMapper.selectByExample(example);
	}
	
}
